package com.example.gmallproduct.service.impl;


import com.common.constant.ProductConstant;
import com.example.gmallproduct.VO.AttrGroupVo;
import com.example.gmallproduct.VO.AttrReleationVo;
import com.example.gmallproduct.VO.AttrResponseVo;
import com.example.gmallproduct.dao.AttrAttrgroupRelationDao;
import com.example.gmallproduct.dao.AttrDao;
import com.example.gmallproduct.entity.AttrAttrgroupRelationEntity;
import com.example.gmallproduct.entity.AttrEntity;
import com.example.gmallproduct.entity.CategoryEntity;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.example.gmallproduct.dao.AttrGroupDao;
import com.example.gmallproduct.entity.AttrGroupEntity;
import com.example.gmallproduct.service.AttrGroupService;
import org.springframework.web.bind.annotation.RequestBody;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrDao attrDao;
    @Autowired
    private AttrGroupDao attrGroupDao;


    @Autowired
    private AttrAttrgroupRelationDao relationDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryByCatId(Map<String, Object> params, Long catId) {
        //catId为0表示查找所有
        String key = (String) params.get("key");
        if (catId == 0 && key != null && key.length() > 0) {
            //catId为0,但是关键子不为空,按关键字查找
            IPage<AttrGroupEntity> iPage = new Query<AttrGroupEntity>().getPage(params);
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>().like("attr_group_name", key).or().like("descript", key).or().eq("attr_group_id", key);
            return new PageUtils(this.page(iPage, wrapper));
        } else if (catId == 0 && (key == null || key.length() == 0)) {
            //三级分类为0,同时key为空,查所有
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    new QueryWrapper<AttrGroupEntity>()
            );
            return new PageUtils(page);
        } else {
            IPage<AttrGroupEntity> iPage = new Query<AttrGroupEntity>().getPage(params);
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catId);
            return new PageUtils(this.page(iPage, wrapper));
        }
    }

    @Override
    public AttrGroupEntity getBySelf(Long attrGroupId) {
        AttrGroupEntity entity = baseMapper.selectById(attrGroupId);
        ArrayList<Long> list = new ArrayList<Long>();
        if (entity == null) {
            return null;
        }
        findPath(entity.getCatelogId(), list);
        Collections.reverse(list);
        Long[] longs = new Long[list.size()];
        int i = 0;
        for (long id : list) {
            longs[i++] = id;
        }
        entity.setCatParent(longs);
        return entity;
    }

    @Override
    public List<AttrResponseVo> getRelationAttr(Long attrgroupId) {
        QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
        ArrayList<AttrResponseVo> responseVoArrayList = new ArrayList<>();
        wrapper.eq("attr_group_id", attrgroupId);
        List<AttrAttrgroupRelationEntity> relationEntityList = attrAttrgroupRelationDao.selectList(wrapper);
        for (AttrAttrgroupRelationEntity entity : relationEntityList) {
            if (entity.getAttrId() != null) {
                QueryWrapper<AttrEntity> wrapper1 = new QueryWrapper<AttrEntity>().eq("attr_id", entity.getAttrId());
                AttrEntity attrEntity = attrDao.selectOne(wrapper1);
                AttrResponseVo responseVo = new AttrResponseVo();
                BeanUtils.copyProperties(attrEntity, responseVo);
                responseVoArrayList.add(responseVo);
            }
        }
        return responseVoArrayList;
    }

    @Override
    public void deleteRelation(@RequestBody AttrReleationVo[] relationEntity) {
        for (AttrReleationVo attrReleationVo : relationEntity) {
            QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("attr_id", attrReleationVo.getAttrId()).eq("attr_group_id", attrReleationVo.getAttrGroupId());
            attrAttrgroupRelationDao.delete(wrapper);
        }

    }

    @Override
    public List<AttrGroupVo> getAttrsBycatelogId(Long catelogId) {
        //用catelogId查出所有的属性分组信息
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId);
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(wrapper);
        //拿着attrGroup的id找关联表查关联的属性
        ArrayList<AttrGroupVo> groupVos = new ArrayList<>();
        for (AttrGroupEntity groupEntity : attrGroupEntities) {
            AttrGroupVo groupVo = new AttrGroupVo();
            BeanUtils.copyProperties(groupEntity, groupVo);
            //中间表,attgroup-id ---> attr-id
            ArrayList<AttrEntity> attrEntityArrayList = null;
            if (groupEntity.getAttrGroupId() != null) {
                QueryWrapper<AttrAttrgroupRelationEntity> wrapper1 = new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", groupEntity.getAttrGroupId());
                List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(wrapper1);
                attrEntityArrayList = new ArrayList<>();
                for (AttrAttrgroupRelationEntity relationEntity : relationEntities) {
                    if (relationEntity.getAttrId() != null) {
                        QueryWrapper<AttrEntity> wrapper2 = new QueryWrapper<AttrEntity>().eq("attr_id", relationEntity.getAttrId());
                        AttrEntity entity = attrDao.selectOne(wrapper2);
                        attrEntityArrayList.add(entity);
                    }
                }
            }
            //将arrayList转换成数组
            AttrEntity[] attrArray = new AttrEntity[attrEntityArrayList.size()];
            for (int i = 0; i < attrArray.length; i++) {
                attrArray[i] = attrEntityArrayList.get(i);
            }
            groupVo.setAttrs(attrArray);
            groupVos.add(groupVo);
        }
        return groupVos;
    }


    @Autowired
    CategoryServiceImpl cs;

    private void findPath(Long id, ArrayList<Long> list) {
        list.add(id);
        CategoryEntity entity = cs.getById(id);
        if (entity.getParentCid() != null && entity.getParentCid() != 0) {
            findPath(entity.getParentCid(), list);
        }
    }

}